import os
from datetime import datetime

import numpy as np
from django.conf import settings
from django_redis import get_redis_connection

def auth(request):
    if request.META.get('HTTP_AUTHORIZATION') is None:
        return True
    varifyToken = request.META.get('HTTP_AUTHORIZATION').split(" ")[1]
    if len(varifyToken)==0:
        return True
    token="40_LCRv6E6YDiQdh_GNaRq4vSQpVN40o5nBq4yDWwTUkabqLwCxmoR78EPUfwsoEpCy4PS0OYSF-3LYJxSmY_oI8i3I6pjEMGQI6nTtsQLLmiI9-IDPvzMlLNx9kCWTBNPT_BypNdz8EjiT5QfWITVjAHAPAC"
    # cache = get_redis_connection('default')
    # obj = cache.hget("ImageAI", "token")
    # if isinstance(obj, np.ndarray):
    #     token = obj.tolist()
    # elif isinstance(obj, bytes):
    #     token = str(obj, encoding='utf-8')
    if varifyToken!=token:
        return True
def saveFormFile(request):
    f = request.FILES['picture']
    if not os.path.exists(settings.MEDIA_ROOT):
        os.makedirs(settings.MEDIA_ROOT, 0o666)
    filepath = os.path.join(settings.MEDIA_ROOT, f.name)
    print(filepath)
    with open(filepath, 'wb') as fp:
        for info in f.chunks():
            fp.write(info)
        fp.close()
    return filepath

def saveBianryFile(request):
    bs=request.body
    if not os.path.exists(settings.MEDIA_ROOT):
        os.makedirs(settings.MEDIA_ROOT, 0o666)
    fname = datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + ".jpg"
    filepath = os.path.join(settings.MEDIA_ROOT, fname)
    print(filepath)
    with open(filepath, 'wb') as fp:
        #这是数组，循环好像不行
        fp.write(bs)
        fp.close()
    return filepath